package com.dong.controller;

import com.dong.service.QueryIidarServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginController {
    @Autowired
    QueryIidarServiceImpl queryIidarService;

    //登录界面
    @RequestMapping("/user/login")
    public String login(@RequestParam("username") String username, @RequestParam("password")String password, Model model){
        System.out.println("进入登录逻辑");
        //获取当前的用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        model.addAttribute("user_name",username);
        //登录令牌
        try {
            subject.login(token);//执行登录方法，如果没有异常就ok了
            System.out.println("登录成功");
            return "dashboard";
        } catch (UnknownAccountException e) {
            model.addAttribute("msg", "用户名错误");
            return "index";
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("msg", "密码错误");
            return "index";
        }
    }


    //注销用户
    @RequestMapping("/user/logout")
    public String logout(){
        System.out.println("进入注销");
        return "redirect:/index.html";
    }


}
